package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.linlinjava.litemall.db.dao.*;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.util.PageResult;
import org.linlinjava.litemall.db.vo.RecommendedBooksVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class LitemallGoodsProductService {
    @Resource
    private LitemallGoodsProductMapper litemallGoodsProductMapper;
    @Resource
    private GoodsProductMapper goodsProductMapper;
    @Autowired
    private LitemallGoodsMapper litemallGoodsMapper;
    @Resource
    private LitemallSubscriptionMapper litemallSubscriptionMapper;
    @Autowired
    private MessageAysnc messageAysnc;
    @Resource
    private LitemallCategoryMapper categoryMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    public List<LitemallGoodsProduct> queryByGid(Integer gid) {
        LitemallGoodsProductExample example = new LitemallGoodsProductExample();
        example.or().andGoodsIdEqualTo(gid).andDeletedEqualTo(false);
        return litemallGoodsProductMapper.selectByExample(example);
    }

    public LitemallGoodsProduct queryByGidAndSpecification(Integer gid, String specification) {
        LitemallGoodsProductExample example = new LitemallGoodsProductExample();
        example.or().andGoodsIdEqualTo(gid).andSpecificationsEqualTo(specification).andDeletedEqualTo(false);
        return litemallGoodsProductMapper.selectOneByExample(example);
    }

    /**
     * 获取可租借得书列表
     *
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    public PageResult<RecommendedBooksVO> queryByOldAndCategoryCanBorrowed(String bookName, Integer page, Integer limit, String sort, String order) {
        Integer begin = page*limit;
        List<RecommendedBooksVO> recommendedBooksVOList = goodsMapper.queryOldAndCanBorrow(begin, limit, sort, order);
        Long total = goodsMapper.countOldAndCanBorrowTotalNumber();
        PageResult<RecommendedBooksVO> pageResult = new PageResult<>(recommendedBooksVOList, page + 1, limit, total);
        return pageResult;
    }

    //todo page size问题


    public LitemallGoodsProduct findById(Integer id) {
        return litemallGoodsProductMapper.selectByPrimaryKey(id);
    }

    public void deleteById(Integer id) {
        litemallGoodsProductMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallGoodsProduct goodsProduct) {
        goodsProduct.setAddTime(LocalDateTime.now());
        goodsProduct.setUpdateTime(LocalDateTime.now());
        litemallGoodsProductMapper.insertSelective(goodsProduct);
    }

    public int count() {
        LitemallGoodsProductExample example = new LitemallGoodsProductExample();
        example.or().andDeletedEqualTo(false);
        return (int) litemallGoodsProductMapper.countByExample(example);
    }

    public void deleteByGid(Integer gid) {
        LitemallGoodsProductExample example = new LitemallGoodsProductExample();
        example.or().andGoodsIdEqualTo(gid);
        litemallGoodsProductMapper.logicalDeleteByExample(example);
    }

    public int addStock(Integer id, Integer num) {
        return goodsProductMapper.addStock(id, num);
    }

    public Integer reduceStock(Integer id, Integer num) {
        return goodsProductMapper.reduceStock(id, num);
    }

    public void updateById(LitemallGoodsProduct product) {
        product.setUpdateTime(LocalDateTime.now());
        litemallGoodsProductMapper.updateByPrimaryKeySelective(product);
    }

    public boolean getByBooksn(@Param("id") Integer id, @Param("userId") Integer userId) {
        LitemallGoodsProduct litemallGoodsProduct = litemallGoodsProductMapper.selectByPrimaryKey(id);
        Integer number = litemallGoodsProduct.getNumber();
        if (number == 0) {
            LitemallSubscription LitemallSubscription = new LitemallSubscription();
            LitemallSubscription.setGoodsProductId(id);
            LitemallSubscription.setAddTime(LocalDateTime.now());
            LitemallSubscription.setUpdateTime(LocalDateTime.now());
            LitemallSubscription.setUserId(userId);
            litemallSubscriptionMapper.insert(LitemallSubscription);
            return true;

        } else {
            return false;
        }
    }

    public List<LitemallGoodsProduct> queryIdIn(List<Integer> Ids, Integer page, Integer limit, String sort, String order) {
        LitemallGoodsProductExample goodsProductExample = new LitemallGoodsProductExample();
        goodsProductExample.or().andIdIn(Ids).andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            goodsProductExample.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        return litemallGoodsProductMapper.selectByExample(goodsProductExample);
    }

    public List<LitemallGoods> queryByCategoryId(Integer categoryId) {
        LitemallGoodsExample example = new LitemallGoodsExample();
        example.or().andCategoryIdEqualTo(categoryId).andDeletedEqualTo(false).andIsOnSaleEqualTo(true);
        return litemallGoodsMapper.selectByExample(example);
    }


    public String onChangeWhenUpadate(@Param("id") Integer goodid) {

        String a = "您的宝贝已经有货";
       /*litemallSubscriptionMapper.deleteByGoodid(goodid);
        messageAysnc.setMessage(a);*/
        return a;
    }


}